package com.beijing.http.url;

import javax.net.ssl.HttpsURLConnection;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @Author Ma Wenqi
 * @Date 2021/3/28 15:24
 * @Version 1.0
 */
public class TestURLDownLoad {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://p1.music.126.net/D8GREdE72ZIxDocuMwWkdg==/109951165772610802.jpg?param=50y50");
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream("wyy.jpg");
        byte[] bytes = new byte[1024];
        int temp = 0;
        while ((temp = inputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, temp);
        }
        //这里关闭流简写，正常需要try-catch-finally
        fileOutputStream.close();
        inputStream.close();
        urlConnection.disconnect();
    }
}
